package com.zyfdroid.bigdialerkit.receivers;

import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import com.zyfdroid.bigdialerkit.MissingCallsWdiget;

import android.os.Handler;

public class CallingChangedReceiver extends BroadcastReceiver {

    public static final String CALL_CHANGED_RECEIVER = "com.zyfdroid.bigdialerkit.CALL_CHANGED_RECEIVER";

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean immediately = CALL_CHANGED_RECEIVER.equals(intent.getAction());
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, MissingCallsWdiget.class));
                MissingCallsWdiget.updateAppWidget(context, appWidgetManager, appWidgetIds);
            }
        },immediately ? 100 : 2000);
    }

    public static void notifyUpdate(Context ctx){
        Intent i = new Intent(CALL_CHANGED_RECEIVER);
        i.setComponent(new ComponentName(ctx, CallingChangedReceiver.class));
        ctx.sendBroadcast(i);
    }
}